package com.uzai.console.service.rebotcmd.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.taobao.api.internal.toplink.embedded.websocket.util.StringUtil;
import com.uzai.common.enums.PluginKeyEnum;
import com.uzai.common.enums.UzaiCmdKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.entity.RobotCmd;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.mapper.RobotCmdMapper;
import com.uzai.console.mapper.ProductMapper;
import com.uzai.console.service.rebotcmd.RobotCmdService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 运营商基本配置
 * @author liuqi
 * @date 2021年08月13日 11:34
 */
@Service
public class RobotCmdServiceImpl implements RobotCmdService {

    Logger logger = LoggerFactory.getLogger(RobotCmdServiceImpl.class);

    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private RobotCmdMapper proAuthMapper;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;
    @Autowired
    private RobotCmdMapper robotCmdMapper;

    /**
     * 保存指令
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param data
     * @return null
     */
    public void addRobotCmdForConfigPlan(Long merId, Long configId, String pluginKey, String data){

        if(StringUtils.isBlank(data)){
            return;
        }

        JSONObject dataObject = JSONObject.parseObject(data);

        //指令对象List
        List<RobotCmd> robotCmdList = new ArrayList<>();
        //订阅消息
        if(pluginKey.equals(PluginKeyEnum.SUBSCRIBE_MSG.getValue())){
            //全部订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ALL_SUBSCRIBE.getKey());
            String SUBSCRIBE_MSG = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ALL_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ALL_SUBSCRIBE.getKey(), SUBSCRIBE_MSG);
            //全部退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ALL_UNSUBSCRIBE.getKey());
            String ALL_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ALL_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ALL_UNSUBSCRIBE.getKey(), ALL_UNSUBSCRIBE);
            //订单付款订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ORDER_PAY_SUBSCRIBE.getKey());
            String ORDER_PAY_SUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ORDER_PAY_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ORDER_PAY_SUBSCRIBE.getKey(), ORDER_PAY_SUBSCRIBE);
            //订单付款退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ORDER_PAY_UNSUBSCRIBE.getKey());
            String ORDER_PAY_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ORDER_PAY_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ORDER_PAY_UNSUBSCRIBE.getKey(), ORDER_PAY_UNSUBSCRIBE);
            //订单失效订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ORDER_INVALID_SUBSCRIBE.getKey());
            String ORDER_INVALID_SUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ORDER_INVALID_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ORDER_INVALID_SUBSCRIBE.getKey(), ORDER_INVALID_SUBSCRIBE);
            //订单失效退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ORDER_INVALID_UNSUBSCRIBE.getKey());
            String ORDER_INVALID_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ORDER_INVALID_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ORDER_INVALID_UNSUBSCRIBE.getKey(), ORDER_INVALID_UNSUBSCRIBE);
            //订单结算订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ORDER_SETTLE_SUBSCRIBE.getKey());
            String ORDER_SETTLE_SUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ORDER_SETTLE_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ORDER_SETTLE_SUBSCRIBE.getKey(), ORDER_SETTLE_SUBSCRIBE);
            //订单结算退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ORDER_SETTLE_UNSUBSCRIBE.getKey());
            String ORDER_SETTLE_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ORDER_SETTLE_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ORDER_SETTLE_UNSUBSCRIBE.getKey(), ORDER_SETTLE_UNSUBSCRIBE);
            //提成-订单付款订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_PAY_SUBSCRIBE.getKey());
            String REWARD_ORDER_PAY_SUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.REWARD_ORDER_PAY_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_PAY_SUBSCRIBE.getKey(), REWARD_ORDER_PAY_SUBSCRIBE);
            //提成-订单付款退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_PAY_UNSUBSCRIBE.getKey());
            String REWARD_ORDER_PAY_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.REWARD_ORDER_PAY_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_PAY_UNSUBSCRIBE.getKey(), REWARD_ORDER_PAY_UNSUBSCRIBE);
            //提成-订单失效订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_INVALID_SUBSCRIBE.getKey());
            String REWARD_ORDER_INVALID_SUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.REWARD_ORDER_INVALID_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_INVALID_SUBSCRIBE.getKey(), REWARD_ORDER_INVALID_SUBSCRIBE);
            //提成-订单失效退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_INVALID_UNSUBSCRIBE.getKey());
            String REWARD_ORDER_INVALID_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.REWARD_ORDER_INVALID_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_INVALID_UNSUBSCRIBE.getKey(), REWARD_ORDER_INVALID_UNSUBSCRIBE);

            //提成-订单结算订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_SETTLE_SUBSCRIBE.getKey());
            String REWARD_ORDER_SETTLE_SUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.REWARD_ORDER_SETTLE_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_SETTLE_SUBSCRIBE.getKey(), REWARD_ORDER_SETTLE_SUBSCRIBE);
            //提成-订单结算退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_SETTLE_UNSUBSCRIBE.getKey());
            String REWARD_ORDER_SETTLE_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.REWARD_ORDER_SETTLE_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.REWARD_ORDER_SETTLE_UNSUBSCRIBE.getKey(), REWARD_ORDER_SETTLE_UNSUBSCRIBE);

            //群发消息订阅
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.BROAD_MSG_SUBSCRIBE.getKey());
            String BROAD_MSG_SUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.BROAD_MSG_SUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.BROAD_MSG_SUBSCRIBE.getKey(), BROAD_MSG_SUBSCRIBE);
            //群发消息退订
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.BROAD_MSG_UNSUBSCRIBE.getKey());
            String BROAD_MSG_UNSUBSCRIBE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.BROAD_MSG_UNSUBSCRIBE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.BROAD_MSG_UNSUBSCRIBE.getKey(), BROAD_MSG_UNSUBSCRIBE);

            //用户个人信息查询
        } else if(pluginKey.equals(PluginKeyEnum.USER_INFO_QUERY.getValue())){
            //用户信息明细查询
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.USER_INFO_DETAIL.getKey());
            String USER_INFO_DETAIL = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.USER_INFO_DETAIL.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.USER_INFO_DETAIL.getKey(), USER_INFO_DETAIL);
            //个人中心卡片获取
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.USER_INFO_CARD.getKey());
            String USER_INFO_CARD = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.USER_INFO_CARD.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.USER_INFO_CARD.getKey(), USER_INFO_CARD);
            //用户订单列表
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.USER_ORDER_LIST.getKey());
            String USER_ORDER_LIST = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.USER_ORDER_LIST.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.USER_ORDER_LIST.getKey(), USER_ORDER_LIST);

            //提现
        }else if(pluginKey.equals(PluginKeyEnum.USER_TAKE_BAL.getValue())){
            //机器人用户提现-微信转账
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL.getKey());
            String ROBOT_USER_TAKE_BAL = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL.getKey(), ROBOT_USER_TAKE_BAL);

            //机器人用户提现-支付宝转账
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_ALIPAY.getKey());
            String ROBOT_USER_TAKE_BAL_ALIPAY = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_ALIPAY.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_ALIPAY.getKey(), ROBOT_USER_TAKE_BAL_ALIPAY);

            //机器人用户提现-微信红包
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_WX_REDPACKET.getKey());
            String ROBOT_USER_TAKE_BAL_WX_REDPACKET = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_WX_REDPACKET.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_WX_REDPACKET.getKey(), ROBOT_USER_TAKE_BAL_WX_REDPACKET);

            //机器人用户提现-手动提现
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_HANDLE.getKey());
            String ROBOT_USER_TAKE_BAL_HANDLE = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_HANDLE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.ROBOT_USER_TAKE_BAL_HANDLE.getKey(), ROBOT_USER_TAKE_BAL_HANDLE);

            //绑定支付宝
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.BIND_ALIPAY_ACCOUNT.getKey());
            String BIND_ALIPAY_ACCOUNT = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.BIND_ALIPAY_ACCOUNT.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.BIND_ALIPAY_ACCOUNT.getKey(), BIND_ALIPAY_ACCOUNT);
            
            //社群管理
        }else if(pluginKey.equals(PluginKeyEnum.COMMUNITY.getValue())){
            //设置为代理
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.SET_AGENT.getKey());
            String SET_AGENT = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.SET_AGENT.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.SET_AGENT.getKey(), SET_AGENT);

            //启动社群
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.START_COMMUNITY.getKey());
            String START_COMMUNITY = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.START_COMMUNITY.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.START_COMMUNITY.getKey(), START_COMMUNITY);

            //停止社群
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.STOP_COMMUNITY.getKey());
            String STOP_COMMUNITY = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.STOP_COMMUNITY.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.STOP_COMMUNITY.getKey(), STOP_COMMUNITY);

            //培训方案
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.TRAINING_TXT.getKey());
            String TRAINING_TXT = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.TRAINING_TXT.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.TRAINING_TXT.getKey(), TRAINING_TXT);

            //用户签到
        }else if(pluginKey.equals(PluginKeyEnum.USER_SIGNIN.getValue())){
            //签到
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.SIGN_IN.getKey());
            String SIGN_IN = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.SIGN_IN.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.SIGN_IN.getKey(), SIGN_IN);

        }else if(pluginKey.equals(PluginKeyEnum.LOCAL_LIVE.getValue())){
            //本地生活集合页
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.LOCAL_LIVE_VIEW.getKey());
            String LOCAL_LIVE_VIEW = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.LOCAL_LIVE_VIEW.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.LOCAL_LIVE_VIEW.getKey(), LOCAL_LIVE_VIEW);

            //美团
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.LOCAL_LIVE_MEITUAN.getKey());
            String LOCAL_LIVE_MEITUAN = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.LOCAL_LIVE_MEITUAN.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.LOCAL_LIVE_MEITUAN.getKey(), LOCAL_LIVE_MEITUAN);

            //饿了么
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.LOCAL_LIVE_ELM.getKey());
            String LOCAL_LIVE_ELM = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.LOCAL_LIVE_ELM.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.LOCAL_LIVE_ELM.getKey(), LOCAL_LIVE_ELM);

        }else if(pluginKey.equals(PluginKeyEnum.SHOPPING_GUIDE.getValue())){
            //导购
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.SHOPPING_GUIDE.getKey());
            String SIGN_IN = Tools.getStr(dataObject.getString(UzaiCmdKeyEnum.SHOPPING_GUIDE.getKey()));
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.SHOPPING_GUIDE.getKey(), SIGN_IN);
            //拼多多
        }else if(pluginKey.equals(PluginKeyEnum.PDD.getValue())){
            //唤醒拼多多app关键词
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.GUIDE_KEY_PDD.getKey());
            String guide_key_pdd = Tools.getStr(dataObject.getString("guide_key")); //拼多多的唤醒app指令key
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.GUIDE_KEY_PDD.getKey(), guide_key_pdd);
        //微信机器人关联公众号插件
        }else if(pluginKey.equals(PluginKeyEnum.WECHAT_UNION_WXPUB.getValue())){
            //关注二维码触发指令
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.WXPUB_QRCODE.getKey());
            String wxpub_qrcode = Tools.getStr(dataObject.getString("wxpub_qrcode")); //关注二维码触发指令
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.WXPUB_QRCODE.getKey(), wxpub_qrcode);

            //互通码触发指令
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.UNIONID_QUERY_CMD.getKey());
            String unionid_query_cmd = Tools.getStr(dataObject.getString("unionid_query_cmd")); //互通码触发指令
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.UNIONID_QUERY_CMD.getKey(), unionid_query_cmd);
        }else if(pluginKey.equals(PluginKeyEnum.USER_EXPAND_FISSION.getValue())){
            //推广裂变
            robotCmdMapper.deleteByKey(merId, configId, UzaiCmdKeyEnum.WECHAT_POSTER.getKey());
            String poster_cmd = Tools.getStr(dataObject.getString("poster_cmd")); //海报指令
            saveRobotCmd(merId, configId, UzaiCmdKeyEnum.WECHAT_POSTER.getKey(), poster_cmd);
        }
    }

    /**
     * 新增指令
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param val
     * @return null
     */
    private void saveRobotCmd(Long merId, Long configId, String key, String val){
        try {
            if(StringUtils.isNotBlank(val)){
                Integer now = DateUtil.getNowTime();
                //验证是单个指令，还是指令LIST
                Object json = new org.springframework.boot.configurationprocessor.json.JSONTokener(Tools.getStr(val)).nextValue();
                if (json instanceof org.springframework.boot.configurationprocessor.json.JSONArray) {  //指令List
                    org.springframework.boot.configurationprocessor.json.JSONArray jsonArray = (org.springframework.boot.configurationprocessor.json.JSONArray) json;
                    for(int i =0 ; i < jsonArray.length() ; i++){
                        String val_tmp = Tools.getStr(jsonArray.getString(i));
                        RobotCmd robotCmd = robotCmdMapper.selectByConfigIdAndVal(configId, val_tmp);
                        if(robotCmd != null){
                            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "指令已经存在");
                        }else{
                            robotCmd = new RobotCmd();
                            robotCmd.setId(IdWorker.getId());
                            robotCmd.setMerId(merId);
                            robotCmd.setConfigId(configId);
                            robotCmd.setKey(key);
                            robotCmd.setVal(val_tmp);
                            robotCmd.setCreateTime(now);
                            robotCmd.setUpdateTime(now);
                            robotCmdMapper.insert(robotCmd);
                        }
                    }
                }else{//单个指令
                    val = Tools.getStr(val);//去空格
                    RobotCmd robotCmd = robotCmdMapper.selectByConfigIdAndVal(configId, val);
                    if(robotCmd != null){
                        throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "指令已经存在");
                    }else{
                        robotCmd = new RobotCmd();
                        robotCmd.setId(IdWorker.getId());
                        robotCmd.setMerId(merId);
                        robotCmd.setConfigId(configId);
                        robotCmd.setKey(key);
                        robotCmd.setVal(val);
                        robotCmd.setCreateTime(now);
                        robotCmd.setUpdateTime(now);
                        robotCmdMapper.insert(robotCmd);
                    }
                }
            }
        }catch (Exception e){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, e.getMessage());
        }
    }

}
